package com.cys.dm.rest.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cys.dm.dao.scheduler.DmSchedulerDao;
import com.cys.dm.dao.scheduler.DmSchedulerSeatDao;
import com.cys.dm.dao.scheduler.DmSchedulerSeatPriceDao;
import com.cys.dm.entity.scheduler.DmScheduler;
import com.cys.dm.entity.scheduler.DmSchedulerSeat;
import com.cys.dm.entity.scheduler.DmSchedulerSeatPrice;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class SchedulerService {

    @Resource
    private DmSchedulerDao dmSchedulerDao;
    @Resource
    private DmSchedulerSeatPriceDao dmSchedulerSeatPriceDao;
    @Resource
    private DmSchedulerSeatDao dmSchedulerSeatDao;

    /**
     * 根据排期ID查询排期价格表
     * @param schedulerId
     * @return
     */
    @PostMapping(path = {"findSchedulerSeatPriceBySchedulerId/{schedulerId}"})
    public List<DmSchedulerSeatPrice> findSchedulerSeatPriceBySchedulerId(@PathVariable("schedulerId")Long schedulerId){
        QueryWrapper<DmSchedulerSeatPrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scheduleId",schedulerId);
        return dmSchedulerSeatPriceDao.selectList(queryWrapper);
    }

    /**
     * 根据商品ID查询排期
     * @param itemId
     * @return
     */
    @PostMapping(path = {"findSchedulerByItemId/{itemId}"})
    public List<DmScheduler> findSchedulerByItemId(@PathVariable("itemId")Long itemId){
        QueryWrapper<DmScheduler> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemId", itemId);
        return dmSchedulerDao.selectList(queryWrapper);
    }

    @PostMapping(path = "/findSchedulerById")
    public List<DmScheduler> findSchedulerById(@RequestParam Long itemId){
        QueryWrapper<DmScheduler> dmSchedulerQueryWrapper = new QueryWrapper<>();
        dmSchedulerQueryWrapper.eq("itemId",itemId);
        return dmSchedulerDao.selectList(dmSchedulerQueryWrapper);
    }

    @PostMapping(path = "/queryItemPrice")
    public List<DmSchedulerSeatPrice> queryItemPrice(@RequestParam Long scheduleId){
        QueryWrapper<DmSchedulerSeatPrice> dmSchedulerSeatPriceQueryWrapper = new QueryWrapper<>();
        dmSchedulerSeatPriceQueryWrapper.eq("scheduleId",scheduleId);
        return dmSchedulerSeatPriceDao.selectList(dmSchedulerSeatPriceQueryWrapper);
    }

    /**
     * 根据剧场ID查询座位信息
     * @param scheduleId 剧场ID
     * @return
     */
    @PostMapping(path = {"/findSchedulerSeatBySchedulerId/{scheduleId}"})
    public List<DmSchedulerSeat> findSchedulerSeatBySchedulerId(@PathVariable("scheduleId")Long scheduleId){
        QueryWrapper<DmSchedulerSeat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scheduleId",scheduleId);
        return dmSchedulerSeatDao.selectList(queryWrapper);
    }

    /**
     * 根据排期ID分组查询X轴的总数
     * @param id
     * @return
     */
    @PostMapping(path = "/findScheduleSeatByIDAndX/{id}")
    public Integer findScheduleSeatByIDAndX(@PathVariable("id") Long id){
        return dmSchedulerSeatDao.findByIdAndX(id);
    }

    /**
     * 根据排期ID分组查询X轴的总数
     * @param id
     * @return
     */
    @PostMapping(path = "/findScheduleSeatByIDAndY/{id}")
    public Integer findScheduleSeatByIDAndY(@PathVariable("id") Long id){
        return dmSchedulerSeatDao.findByIdAndY(id);
    }

    /**
     * 根据排期ID座位经纬度查询座位
     * @param id 排期ID
     * @param x 经度
     * @param y 维度
     * @return
     */
    @PostMapping(path = {"/findSchedulerSeatByIdAndXAndY/{id}/{x}/{y}"})
    public DmSchedulerSeat findSchedulerSeatByIdAndXAndY(@PathVariable("id")Long id,
                                                         @PathVariable("x")Integer x,
                                                         @PathVariable("y")Integer y){
        QueryWrapper<DmSchedulerSeat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scheduleId",id);
        queryWrapper.eq("x",x);
        queryWrapper.eq("y",y);
        return dmSchedulerSeatDao.selectOne(queryWrapper);
    }

    /**
     * 根据排期和座位规格查询价格
     * @param scheduleId
     * @param level
     * @return
     */
    @PostMapping(path = "/findSeatPriceByScheduleIdAndLevel/{scheduleId}/{level}")
    public DmSchedulerSeatPrice findSeatPriceByScheduleIdAndLevel( @PathVariable Long scheduleId,
                                                                   @PathVariable Long level){
        QueryWrapper<DmSchedulerSeatPrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scheduleId",scheduleId);
        queryWrapper.eq("areaLevel",level);
        return dmSchedulerSeatPriceDao.selectOne(queryWrapper);
    }

    /**
     * 根据排期ID和经纬度修改座位信息
     * @param dmSchedulerSeat
     * @return
     */
    @PostMapping(path = "/updateSeatByXAndYAndScheduleId")
    public Integer updateSeatByXAndYAndScheduleId(@RequestBody DmSchedulerSeat dmSchedulerSeat){
        QueryWrapper<DmSchedulerSeat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("x",dmSchedulerSeat.getX());
        queryWrapper.eq("y",dmSchedulerSeat.getY());
        queryWrapper.eq("scheduleId",dmSchedulerSeat.getScheduleId());
        return dmSchedulerSeatDao.update(dmSchedulerSeat, queryWrapper);
    }

    /**
     * 根据订单号修改座位状态
     * @param dmSchedulerSeat
     * @return
     */
    @PostMapping("updSeatStatus")
    public Integer updSeatStatus(@RequestBody DmSchedulerSeat dmSchedulerSeat){
        QueryWrapper<DmSchedulerSeat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderNo",dmSchedulerSeat.getOrderNo());
        return dmSchedulerSeatDao.update(dmSchedulerSeat,queryWrapper);
    }

    /**
     * 根据订单编号查询排期座位信息
     * @param orderNo 订单编号
     * @return
     */
    @PostMapping(path = {"findSchedulerSeatByOrderNo/{orderNo}"})
    public List<DmSchedulerSeat> findSchedulerSeatByOrderNo(@PathVariable("orderNo")String orderNo){
        QueryWrapper<DmSchedulerSeat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderNo",orderNo);
        return dmSchedulerSeatDao.selectList(queryWrapper);
    }


}
